package com.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baidu.ueditor.ActionEnter;
import com.entity.SysUser;
import com.service.IArticleService;
import com.service.ISysUserService;

@Controller
@RequestMapping(value = "/home")
public class HomeController extends BaseController {

	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private IArticleService articleService;

	/*
	 * 进入个人主页
	 */
	@RequestMapping(value = "/{username}")
	public String index(Model model, @PathVariable("username") String username) {
		try {
			SysUser sysUser = sysUserService.findByUserName(username);
			if (sysUser == null) {
				model.addAttribute("message", "该用户不存在");
				return "error";
			}

			model.addAttribute("articleList", articleService.getArticleListByUserId(sysUser.getId()));
			model.addAttribute("homeName", username);

			return "home/index";
		} catch (Exception e) {
			log.error(e);
		}
		return "error";
	}

	/*
	 * 百度富文本编辑器文件上传处理中心
	 */
	@RequestMapping(value = "/ueditor")
	public void ueditor(HttpServletRequest request, HttpServletResponse response) {
		try {
			request.setCharacterEncoding("utf-8");
			response.setHeader("Content-Type", "text/html");
			String roolPath = request.getSession().getServletContext().getRealPath("/");
			String configStr = new ActionEnter(request, roolPath).exec();
			response.getWriter().write(configStr);
		} catch (Exception e) {
			log.error(e);
		}
	}
}
